<?php

/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-18
 * Time: 下午11:39
 */
class Activity extends MY_Api_Controller
{
    protected $_page_size;
    function __construct(){
        parent::__construct();
        $this->_page_size = $this->config->item('page_size','default');
    }

    /**
     * 获取活动列表  GET
     *
     * @return json
     */
    function gets(){
        $page = $this->input->get('page');
        $recommend = $this->input->get('recommend');
        $status = $this->input->get('status');
        $status = empty($status)?'recommend':$status;

        if((empty($page) and $status=='recommend') or !empty($recommend)){
            $this->config->load('data/recommend');
            $activity = @$this->config->item('index')['recommend_activity'];
        }else{
            $page = $page>0 ? $page : 1;
            $field  = $this->input->get('field');
            $field  = empty($field)?'id':$field;
            $sort   = $this->input->get('sort')=='asc'?'asc':'desc';

            $condition = array(
                'is_del'    =>  0,
                'is_sold'   =>  1,
            );
            $options = array(
                'limit' =>array(
                    $this->_page_size,
                    ($page-1)*$this->_page_size
                ),
                'order'=>
                    array($field.' '.$sort)
            );

            if($status=='history'){
                $condition['is_sold'] = 0;
            }

            $this->load->model('activities/Activities_model');

            $activity = $this->Activities_model->get_activities(
                $condition,
                array('title','cover','intro','content','price','special_info','places','sold','start_time','created'),
                $options
            );

        }
        if(!$activity){
            $this->set_response(10004);
        }

        $this->load->helper('image');
        foreach($activity as $key=>&$value){
            if((isset($value['lecturer']) and !empty($value['lecturer_id']))){
                if(!isset($value['address_info'])){
                    unset($activity[$key]);
                    continue;
                }
                $le_avatar = $value['lecturer']['avatar'];
                $ad_avatar =  $value['address_info']['cover'];
            }else if(isset($value['lecturer_avatar'])){
                $le_avatar = $value['lecturer_avatar'];
                $ad_avatar =  $value['place_avatar'];
            }else{
                unset($activity[$key]);
                continue;
            }

            $value['lecturer']['avatar'] = get_small_image($le_avatar,'lecturer');
            $value['address_info']['cover'] = get_small_image($ad_avatar,'place');
            $value['cover'] = get_small_image($value['cover'],'_middle');
            $value = $this->machining($value,array(
                'content_key'   =>  'content',
            ));

            unset($value);
        }


        $this->set_response(0,$activity);
    }

    /**
     * 获取活动信息
     */
    function get(){
        $activity_id = $this->input->get('activity_id ');
        $condition = array('is_del'=>0);
        if($activity_id<0){
            $this->set_response(10404);
        }
        $this->load->model('activities/Activities_model');
        $this->load->model('place/Place_model');
        $this->load->model('lecturer/Lecturer_model');
        $condition['id'] = $activity_id;
        $activity = $this->Activities_model->getOne($condition);
        $activity['place'] = $this->Place_model->getOne(array('id'=>$activity['place_id']));
        $activity['lecturer'] = $this->Lecturer_model->getOne(array('id'=>$activity['lecturer_id']));
        if(!$activity)
            $this->set_response(10004);
         $this->set_response(0,$activity);
    }
    /**
     *  获取推荐活动（内容推荐）
     */
    function get_recommend(){
        $this->config->load('data/recommend');
        $this->set_response(0,$this->config->item('content')['recommend_activity']);
    }

}